package name.fowler.refactoring.simplestore;

import name.fowler.refactoring.simplestore.calculator.FreqPointCalculator;
import name.fowler.refactoring.simplestore.calculator.RentalAmountCalculator;
import name.fowler.refactoring.simplestore.domain.Customer;
import name.fowler.refactoring.simplestore.domain.Rental;

public class Reporter {
	private Customer customer;

	public Reporter(Customer customer) {
		this.customer = customer;
	}

	public String createReport(FreqPointCalculator fpCalc,
			RentalAmountCalculator raCalc) {
		double totalAmount = raCalc.calculateRentalAmountForAll(customer.getRentals());
		int freqPoint = fpCalc.calculateRentalFreqPointForAll(customer.getRentals());

		String result = "Rental record for " + customer.getName() + "\n";
		for (Rental rental : customer.getRentals()) {
			result += "\t" + rental.getMovie().getTitle() + "\t"
					+ raCalc.calculateRentalAmount(rental) + "\n";
		}

		result += createCommonRentalResult(totalAmount, freqPoint, result);
		return result;
	}

	public String createCommonRentalResult(double totalAmount, int freqPoint,
			String result) {
		return new StringBuilder()
				.append("Amount owed is " + totalAmount + "\n")
				.append("You earned " + freqPoint + " frequent renter points.")
				.toString();
	}

}